#pragma once
#include<vector>
#include<assert.h>

namespace syz
{
	template<class T>
	struct ListNode
	{
		ListNode<T>* _next;
		ListNode<T>* _prev;
		T _data;

		ListNode(const T& x = T())
			:_next(nullptr)
			, _prev(nullptr)
			, _data(x)
		{}
	};

	template<class T, class Ref, class Ptr>
	struct __list_iterator
	{
		typedef ListNode<T> Node;
		typedef __list_iterator<T, Ref, Ptr> self;

		Node* _node;

		__list_iterator(Node* node = nullptr)
			:_node(node)
		{}

		//?
		//__list_iterator(const self& it)
		//	:_node(it._node)
		//{
		//	cout << "iterator&" << endl;
		//}

		Ref operator*()
		{
			return _node->_data;
		}

		Ptr operator->()
		{
			return &_node->_data;
		}

		self& operator++()
		{
			_node = _node->_next;

			return *this;
		}

		self operator++(int)
		{
			self tmp(*this);
			_node = _node->_next;

			return tmp;
		}

		self& operator--()
		{
			_node = _node->_prev;

			return *this;
		}

		self operator--(int)
		{
			self tmp(*this);
			_node = _node->_prev;

			return tmp;
		}

		bool operator==(const self& it)
		{
			return _node == it._node;
		}

		bool operator!=(const self& it)
		{
			return !(*this == it);
		}
	};

	template<class Iterator,class Ref,class Ptr>
	struct Reverse_iterator
	{
		Iterator _it;
		typedef Reverse_iterator<Iterator, Ref, Ptr> self;

		Reverse_iterator(Iterator it)
			:_it(it)
		{}

		Ref operator*()
		{
			Iterator tmp = _it;
			return *(--tmp);
		}

		Ptr operator->()
		{
			return &(*this);
		}

		self& operator++()
		{
			--_it;
			return *this;
		}
		
		self& operator--()
		{
			++_it;
			return *this;
		}

		bool operator!=(const self& it)
		{
			return _it != it._it;
		}
	};

	template<class T>
	class list
	{
		typedef ListNode<T> Node;
	public:
		typedef __list_iterator<T, T&, T*> iterator;
		typedef __list_iterator<T, const T&, const T*> const_iterator;

		typedef Reverse_iterator<iterator, T&, T*> reverse_iterator;
		typedef Reverse_iterator<const_iterator, const T&, const T*> const_reverse_iterator;

		iterator begin()
		{
			return iterator(_head->_next);
		}

		iterator end()
		{
			return iterator(_head);
		}

		const_iterator begin() const
		{
			return const_iterator(_head->_next);
		}

		const_iterator end() const
		{
			return const_iterator(_head);
		}

		reverse_iterator rbegin()
		{
			return reverse_iterator(end());
		}

		reverse_iterator rend()
		{
			return reverse_iterator(begin());
		}

		const_reverse_iterator rbegin() const
		{
			return const_reverse_iterator(end());
		}

		const_reverse_iterator rend() const
		{
			return const_reverse_iterator(begin());
		}

		void empty_init()
		{
			_head = new Node;
			_head->_next = _head;
			_head->_prev = _head;
		}

		void swap(list<T>& l)
		{
			std::swap(_head, l._head);
		}

		list()
		{
			empty_init();
		}

		list(int n, const T& val = T())
		{
			empty_init();
			while (n--)
			{
				push_back(val);
			}
		}

		template<class Iterator>
		list(Iterator first, Iterator last)
		{
			empty_init();
			while (first != last)
			{
				push_back(*first++);
			}
		}

		list(const list<T>& l)
		{
			empty_init();

			list<T> tmp(l.begin(), l.end());
			swap(tmp);
		}

		~list()
		{
			//clear();

			delete _head;
			_head = nullptr;
		}

		list<T>& operator=(list<T> l)
		{
			swap(l);
			return *this;
		}

		size_t size() const
		{
			size_t size = 0;
			const_iterator it = begin();

			while (it != end())
			{
				++it;
				++size;
			}

			return size;
		}

		bool empty() const
		{
			return size() == 0;
		}

		T& front()
		{
			return _head->_next->_data;
		}

		const T& front()const
		{
			return _head->_next->_data;
		}

		T& back()
		{
			return _head->_prev->_data;
		}

		const T& back()const
		{
			return _head->_prev->_data;
		}

		iterator insert(iterator pos, const T& val)
		{
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* newnode = new Node(val);

			prev->_next = newnode;
			newnode->_prev = prev;
			cur->_prev = newnode;
			newnode->_next = cur;

			return iterator(newnode);
		}

		iterator erease(iterator pos)
		{
			Node* cur = pos._node;
			Node* prev = cur->_prev;
			Node* next = cur->_next;

			prev->_next = next;
			next->_prev = prev;
			delete cur;

			return next;
		}

		void push_front(const T& val)
		{
			insert(begin(), val);
		}

		void push_back(const T& val)
		{
			//Node* newnode = new Node(val);
			//Node* tail = _head->_prev;

			//tail->_next = newnode;
			//newnode->_prev = tail;
			//_head->_prev = newnode;
			//newnode->_next = _head;

			insert(end(), val);
		}

		void pop_front()
		{
			erease(begin());
		}

		void pop_back()
		{
			erease(--end());
		}

		void clear()
		{
			iterator it = begin();
			while (it != end())
			{
				it = erease(it);
			}
		}

	private:
		Node* _head;
	};

	void test_list1()
	{
		list<int> l1(10, 1);
		list<int> l2(++l1.begin(), --l1.end());

		for (auto e : l2)
		{
			cout << e << " ";
		}
	}

	void test_list2()
	{
		list<string> l(3, "aaa");
		l.push_back("bbb");
		l.push_back("ccc");
		l.push_back("ddd");

		for (auto e : l)
		{
			cout << e << " ";
		}
	}

	void test_list3()
	{
		list<int> l1(10, 1);
		list<int>l2 = l1;

		for (auto e : l2)
		{
			cout << e << " ";
		}
	}

	void test_list4()
	{
		list<int> l(3, 5);
		l.push_back(6);
		l.push_back(7);
		l.push_back(8);

		for (auto e : l)
		{
			cout << e << " ";
		}
		cout << endl;
		cout << l.size() << endl;

		l.push_front(4);
		l.push_front(3);
		l.push_front(2);

		for (auto e : l)
		{
			cout << e << " ";
		}
		cout << endl;
		cout << l.size() << endl;

		l.pop_back();
		l.pop_back();
		l.pop_back();
		for (auto e : l)
		{
			cout << e << " ";
		}
		cout << endl;
		cout << l.size() << endl;

		l.pop_front();
		l.pop_front();
		l.pop_front();
		for (auto e : l)
		{
			cout << e << " ";
		}
		cout << endl;
		cout << l.size() << endl;

		list<int>::iterator it = l.begin();
		while (!l.empty())
		{
			it = l.erease(l.begin());
		}

		cout << endl;
		cout << l.size() << endl;
	}

	void test_list5()
	{
		list<int> l(5, 1);

		l.front() = 0;
		l.back() = 4;
		for (auto e : l)
		{
			cout << e << " ";
		}
		cout << endl;
	}

	void test_list6()
	{
		const list<int> l(5, 1);
		list<int>::const_iterator it;
		cout << l.back() << endl;
		it = l.begin();

		while (it != l.end())
		{
			cout << *it << ' ';
			it++;
		}
	}

	void test_list7()
	{
		list<int> l(5,1);
		l.pop_back();
		l.pop_front();
	}

	void test_list8()
	{
		list<int> l(0);
		l.push_back(1);
		l.push_back(2);
		l.push_back(3);
		l.push_back(4);
		l.push_back(5);

		list<int>::reverse_iterator rit = l.rbegin();
		while (rit != l.rend())
		{
			cout << *rit << ' ';
			++rit;
		}
	}
}


